package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.patchmanage.back.annotation.OperLog;
import com.patchmanage.back.entity.RoleMenuRelevance;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IRoleMenuRelevanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-04-14
 */
@Api(value = " 角色权限关联表 controller", tags = {"角色权限关联表 Api"})
@RestController
@RequestMapping("/back/rolemenurelevance")
public class RoleMenuRelevanceController {

    @Autowired
    public IRoleMenuRelevanceService roleMenuRelevanceService;

    @ApiOperation(value = "新增角色权限关联接口", notes = "新增角色权限关联接口")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @OperLog(operModul="角色权限关联",operDesc="新增角色权限关联")
    public Result<Boolean> saveOrUpdate(@RequestBody RoleMenuRelevance roleMenuRelevance){

        QueryWrapper<RoleMenuRelevance> q = new QueryWrapper();
        q.eq("role_id",roleMenuRelevance.getRoleId());

        List<RoleMenuRelevance> roleMenuRelevanceList= roleMenuRelevanceService.list(q);
        if(roleMenuRelevanceList.size() > 0){
            RoleMenuRelevance r = roleMenuRelevanceList.get(0);
            r.setMenuNames(roleMenuRelevance.getMenuNames());
            UpdateWrapper u = new UpdateWrapper();
            u.eq("role_id",r);
            roleMenuRelevanceService.update(r,u);
        }else{
            roleMenuRelevanceService.save(roleMenuRelevance);
        }
        return Result.success(roleMenuRelevanceService.saveOrUpdate(roleMenuRelevance));
    }

    @ApiOperation(value = "获取角色权限关联接口", notes = "获取角色权限关联接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public Result<RoleMenuRelevance> getById(@RequestBody Dict dict){

        QueryWrapper<RoleMenuRelevance> q = new QueryWrapper();
        q.eq("role_id",dict.getInt("roleId"));
        return Result.success(roleMenuRelevanceService.list(q).get(0));
    }
}
